const jwt = require('jsonwebtoken');
const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
const router = new Router();
const secret = 'secret';
const token = jwt.sign({
    name: 'test',
    age: 18
}, secret,{ expiresIn: 60 * 60 });

router.post('/test', async (ctx, next) => {
    const token = ctx.request.headers.authorization.replace('Bearer ', '');
    try {
        const result = jwt.verify(token, secret);
        console.log(result);
        ctx.body = {
            code: 200,
            data: result
        }
    } catch (e) {
        ctx.body = {
            code: 500,
            data: e,
            message: 'token验证失败'
        }
    }
});
app.use(router.routes());

app.listen(3000, () => {
    console.log('server is running at port 3000');
})